/*
 WindowAdditions.m
 iSpeLL
 
 Created by liulei on 5/31/10.
 Copyright 2010 cool8jay@qq.com. All rights reserved.
 */

#import "WindowAdditions.h"

@implementation NSWindow (WindowAdditions)

/*
 toolbarHeight
 
 Returns the height of the currently visible toolbar.
 */
- (CGFloat)toolbarHeight
{
    NSRect windowFrame;
	CGFloat toolbarHeight = 0.0;
	
    if (([self toolbar]) && ([[self toolbar] isVisible])) {
        windowFrame = [NSWindow contentRectForFrameRect:[self frame] styleMask:[self styleMask]];
        
		toolbarHeight = NSHeight(windowFrame) - NSHeight([[self contentView] frame]);
    }
	
    return toolbarHeight;
}

/*
 resizeForContentView:titleBarVisible
 
 Resizes this window to the size of the supplied view.
 */
- (void)resizeForContentView:(NSView *)view titleBarVisible:(BOOL)visible
{	
	NSSize viewSize = [view frame].size;
	NSRect frame	= [self frame];
	
	if ((viewSize.height) < [self contentMinSize].height) {
		viewSize.height = [self contentMinSize].height;
	}
	
	CGFloat newHeight = (viewSize.height + [self toolbarHeight]);
	
		// If the title bar is visible add 22 pixels to new height of window.
		// the title bar is the bar that contains "close", "minimize" and "resize" buttons and title of window.
	if (visible) { 
        newHeight += 22; 
    }
	
	frame.origin.y += frame.size.height - newHeight;
	
	frame.size.height = newHeight;
	frame.size.width  = viewSize.width; 
	
	[self setFrame:frame display:YES animate:YES];
}
@end
